import time
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

class TestRegistrationLogin:
    """测试用户注册和登录功能"""

    def test_user_registration_and_login(self):
        """测试用户注册后登录流程"""
        # 创建浏览器驱动
        driver = webdriver.Chrome()
        try:
            # 获取访问页面
            driver.get("http://localhost:8080/EasyBuy/Home?action=index")
            # 放大浏览器
            driver.maximize_window()
            # 模拟用户手机注册操作
            time.sleep(2)
            #点击注册
            driver.find_element(By.XPATH,"html/body/div[1]/div/span[2]/span/a[2]").click()
            time.sleep(2)

            #用户名
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[2]/td[2]/input").send_keys("打开4")
            time.sleep(2)

            #密码
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[3]/td[2]/input").send_keys("xjl021022")
            time.sleep(2)

            #确认密码
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[4]/td[2]/input").send_keys("xjl021022")
            time.sleep(2)

            #真实姓名
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[5]/td[2]/input").send_keys("谢啊")
            time.sleep(2)

            #性别"男"
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[6]/td[2]/input[1]").click()
            time.sleep(2)

            #身份证号码
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[7]/td[2]/input").send_keys("123456789123456789")
            time.sleep(2)

            #邮箱
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[8]/td[2]/input").send_keys("2271679600@qq.com")
            time.sleep(2)

            #手机号
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[9]/td[2]/input").send_keys("14777562331")
            time.sleep(2)

            #立即注册
            driver.find_element(By.XPATH,".//*[@id='register']/table/tbody/tr[10]/td[2]/input").click()
            time.sleep(2)

            # -----------登录--------------------------------------

            #用户名
            driver.find_element(By.XPATH,".//*[@id='loginName']").send_keys("打开2")
            time.sleep(2)

            #密码
            driver.find_element(By.XPATH,".//*[@id='password']").send_keys("xjl021022")
            time.sleep(2)

            driver.find_element(By.XPATH,"html/body/div[4]/div[2]/div[2]/form/table/tbody/tr[4]/td[2]/input").click()
            time.sleep(2)
        finally:
            driver.quit()